<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$install = check_account_permission('jy_cashier');
if(!$install){
		exi('暂不支持收银台,还未安装相关插件！','error');		
}
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_SESSION['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid'],'storeid'=>$this->store_id);
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	
	$fields=array('id','status','name','mobile','createtime','title');
    $orderby='createtime desc';
	$total='';
	$return['list']=pdo_getslice('jy_cashier_user',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$return['status']=array('0'=>'关闭','1'=>'开启');
	$url=custom_url('jy_cashier/manage.login/',true);
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'收银台管理'),
			array('op'=>'edit','title'=>'添加/编辑'),
			
		),
		'list'=>array('data'=>'list','tip'=>'收银员登录地址：'.$url,'list'=>array(
			array('name'=>'title','title'=>'收银台名称','type'=>'string'),
								
			array('name'=>'name','title'=>'联系人','type'=>'string'),
			array('name'=>'mobile','title'=>'电话','type'=>'string'),
			
			array('name'=>'createtime','title'=>'注册时间','type'=>'string'),								
			
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),			
			
		),
	
		),
		'search'=>array(
			
			array('name'=>'title','width'=>15,'title'=>'名称','type'=>'string'),
			
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_cashier_user',array('id'=>$id));
		unset($item['password']);
		$set=json_decode($item['set'],true);
		$item['printer']=$set['printer'];
		$item['printer_status']=$set['printer_status'];
		$item['printer_template_default']=$set['printer_template_default'];
		$item['printer_template']=$set['printer_template'];
		$item['credit1']=$set['credit1'];
		$item['credit1_double']=$set['credit1_double'];
		$item['time']=array('tstart'=>$item['lifetimestart'],'tend'=>$item['lifetimeend']);
		
	}else{
		$item=array();
	}
	$return['status']=array('0'=>'关闭','1'=>'开启');
    $return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'desc'=>'参数设置',
		'post'=>'post',
		'edit_tab'=>true,
		'edits'=>array(
			array(
				'title'=>'基础设置',
				'list'=>array(
					array('name'=>'title','title'=>'收银台名称','type'=>'string'),
					//array('name'=>'storeid','title'=>'所属门店','type'=>'select','options'=>'store'),
					array('name'=>'merchid','title'=>'所属商户','type'=>'select','options'=>'merch','option_title'=>'merchname'),
					array('type'=>'image','title'=>'手机端顶部banner','name'=>'logo'),
					array('type'=>'radio','title'=>'是否开启分销','name'=>'isopen_commission','options'=>'status'),
					array('name'=>'name','title'=>'联系人','type'=>'string'),
					array('name'=>'mobile','title'=>'联系电话','type'=>'string'),
					array('name'=>'categoryid','title'=>'选择分类','type'=>'select','options'=>'category'),
				)
			),
			array(
				'title'=>'打印设置',
				'list'=>array(
					array('type'=>'radio','title'=>'是否打印小票','name'=>'printer_status','options'=>'status'),
					array('name'=>'printer','title'=>'选择打印机','type'=>'select','options'=>'printer','help'=>'前往设置添加，打印机：<a href="./index.php?plugin=jy_print&action=print">./index.php?plugin=jy_print&action=print</a>'),
					array('name'=>'printer_template','title'=>'商品收款模板','type'=>'select','options'=>'templateid'),
					array('name'=>'printer_template_default','title'=>'普通收款模板','type'=>'select','options'=>'templateid'),
				)
			),
			array(
				'title'=>'营销设置',
				'list'=>array(
					array('type'=>'radio','title'=>'消费送积分','name'=>'credit1','options'=>'status'),
					array('name'=>'credit1_double','title'=>'积分倍率','type'=>'string','help'=>'积分赠送倍率 默认为空 或者 0 的时候为1倍;'),		
			),
			),
			array(
				'title'=>'结算',
				'list'=>array(
						array('type'=>'radio','title'=>'提现','name'=>'can_withdraw','options'=>'status'),
						array('name'=>'withdraw','title'=>'扣点比例','type'=>'string','help'=>'如果使用的是系统默认支付,扣点比例在申请结算时生效!'),
						
						array('type'=>'choose','name'=>'mid','title'=>'收款微信号','action'=>'get_member'),						
					)
			),
			array(
				'title'=>'账户设置',
				'list'=>array(
					array('name'=>'username','title'=>'后台登陆账户名','type'=>'string'),
					array('name'=>'password','title'=>'后台登陆密码','type'=>'string'),
					array('type'=>'choose','name'=>'managemid','title'=>'绑定管理员微信号','action'=>'get_member'),
					array('type'=>'choose','name'=>'management','title'=>'手机端管理','action'=>'get_member'),
				
					array('type'=>'time_range','title'=>'使用期限','name'=>'time','help'=>''),
					array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
								
					)
			),
			
		
		
		)
	);
	$return['printer']=pdo_getall('jy_weishop_printer',array('uniacid'=>$_SESSION['uniacid']),array('id','title'));
	$return['templateid']=pdo_getall('jy_weishop_printer_template',array('uniacid'=>$_SESSION['uniacid']),array('id','title'));
	$return['merch']=pdo_getall('jy_merch_user',array('uniacid'=>$_SESSION['uniacid']),array('id','merchname'));
	$return['store']=pdo_getall('jy_weishop_store',array('uniacid'=>$_SESSION['uniacid']),array('id','title'));
	$return['category']=pdo_getall('jy_cashier_category',array('uniacid'=>$_SESSION['uniacid']),array('id','title'));
	
	$return['htmls']=$htmls;
	exi($return);
}

if($_RQ['op']=='post'){
	$post=get_postdata();
	$data=array_fields(array('title','merchid','logo','isopen_commission','categoryid','name','mobile','withdraw','can_withdraw','username','mid','managemid','management','status'),$post);
	if($post['password']){
		$data['password']=base64_encode($post['password']);
	}
	$data['lifetimestart']=$post['time']['tstart'];
	$data['lifetimeend']=$post['time']['tend'];
	$data['set']=array('printer'=>$post['printer'],'printer_status'=>$post['printer_status'],'printer_template_default'=>$post['printer_template_default'],'printer_template'=>$post['printer_template'],'credit1'=>$post['credit1'],'credit1_double'=>$post['credit1_double']);
	$data['set']=json_encode($data['set']);
	$id=intval($post['id']);
	if($id){
				pdo_update('jy_cashier_user',$data,array('id'=>$id));
				exi('设置成功！','','/list/');
	}else{
			    $data['uniacid']=$_SESSION['uniacid'];
				$data['createtime']=time();
				$data['storeid']=$this->store_id;
				pdo_insert('jy_cashier_user',$data);
				exi('添加成功！','','/list/');
	}
	exi('设置成功','','list');
}
if($_RQ['op']=='del'){

	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_cashier_user',array('id'=>$id));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_cashier_user',array('id'=>$id));	
			}
		}
	}
	exi('删除成功！');
}
?>